package dxo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import dto.Point;
import dto.Sample;

public class SampleParser {
	public static Sample parse2(String line) {
		
		Sample sample = new Sample();
		String [] tokens = line.split("(\\s|\\t)+");
		sample.setLabel(Integer.parseInt(tokens[0]));
		
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		for(int i = 1; i < tokens.length; i++) {
			String [] pair = tokens[i].split(":");
			map.put(Integer.parseInt(pair[0]), Double.parseDouble(pair[1]));
		}
		
		Point point = new Point();
		point.setLinearSpacePoint(map);
		sample.setPoint(point);

		return sample;
	}
	
	public static Sample parse1(String line) {
		String [] tokens = line.split("(\\s|\\t)+|:");

		Sample sample = new Sample();
		sample.setLabel(Integer.parseInt(tokens[0]));
		
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		for(int i = 1; i < tokens.length; i += 2) {
			map.put(Integer.parseInt(tokens[i]), Double.parseDouble(tokens[i + 1]));
		}
		
		Point point = new Point();
		point.setLinearSpacePoint(map);
		sample.setPoint(point);

		return sample;
	}
	
	public static Sample parse3(String line) {
		
		List<Integer> points = new LinkedList<Integer>();
	
		int cur = 0;
		while(true) {
			cur = line.indexOf(' ', cur + 1);
			if(cur == -1) {
				break;
			}
			points.add(cur);
			cur = line.indexOf(':', cur + 1);
			points.add(cur);
		}
		
		Sample sample = new Sample();
		sample.setLabel(line.charAt(0) - '0');
		
		int [] ps = new int[points.size()];
		int index = 0;
		for(int i : points) {
			ps[index] = i;
			index ++;
		}

		Map<Integer, Double> map = new HashMap<Integer, Double>();
		for(int i = 0; i < ps.length - 1; i += 2) {

			int from = ps[i] + 1;
			int to = ps[i + 1];
			int key = Integer.parseInt(line.substring(from, to));
			from = to + 1;
			to = i == ps.length - 2 ? line.length() : ps[i + 2];
			double value = Double.parseDouble(line.substring(from, to));
			map.put(key, value);
		}
		
		Point point = new Point();
		point.setLinearSpacePoint(map);
		sample.setPoint(point);

		return sample;
	}
	
	public static Sample parse(String line) {
		
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		int first = 1;
		while(first + 1 < line.length() && line.charAt(first + 1) == ' ') {
			first ++;
		}
		
		while(first < line.length()) {
			int second = line.indexOf(':', first + 1);
			
			if(second == -1) {
				break;
			}
			
			int third = line.indexOf(' ', second + 1);
			if(third == -1) {
				third = line.length();
			}
			int key = Integer.parseInt(line.substring(first + 1, second));
			double value = Double.parseDouble(line.substring(second + 1, third));
			map.put(key, value);
			first = third;
		}
		
		Sample sample = new Sample();
		sample.setLabel(line.charAt(0) - '0');
	
		Point point = new Point();
		point.setLinearSpacePoint(map);
		sample.setPoint(point);

		return sample;
	}
}
